package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysPlan;
import com.ruoyi.system.entity.SysEnvironmentPoint;
import com.ruoyi.system.entity.SysGasPoint;
import com.ruoyi.system.mapper.SysEnvironmentPointMapper;
import com.ruoyi.system.mapper.SysPlanMapper;
import com.ruoyi.system.service.SysEnvironmentPointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * (SysEnvironmentPoint)表服务实现类
 *
 * @author makejava
 * @since 2022-12-01 16:50:30
 */
@Service("sysEnvironmentPointService")
public class SysEnvironmentPointServiceImpl implements SysEnvironmentPointService {
    @Resource
    private SysEnvironmentPointMapper sysEnvironmentPointMapper;
    @Autowired
    private SysPlanMapper sysPlanMapper;
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SysEnvironmentPoint queryById(Integer id) {
        return this.sysEnvironmentPointMapper.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param sysEnvironmentPoint 筛选条件
     * @return 查询结果
     */
    @Override
    public List<SysEnvironmentPoint> queryByPage(SysEnvironmentPoint sysEnvironmentPoint) {
        return this.sysEnvironmentPointMapper.queryAllByLimit(sysEnvironmentPoint);
    }

    /**
     * 新增数据
     *
     * @param sysEnvironmentPoint 实例对象
     * @return 实例对象
     */
    @Override
    public SysEnvironmentPoint insert(SysEnvironmentPoint sysEnvironmentPoint) {
        this.sysEnvironmentPointMapper.insert(sysEnvironmentPoint);
        return sysEnvironmentPoint;
    }

    /**
     * 修改数据
     *
     * @param sysEnvironmentPoint 实例对象
     * @return 实例对象
     */
    @Override
    public SysEnvironmentPoint update(SysEnvironmentPoint sysEnvironmentPoint) {
        this.sysEnvironmentPointMapper.update(sysEnvironmentPoint);
        return this.queryById(sysEnvironmentPoint.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.sysEnvironmentPointMapper.deleteById(id) > 0;
    }

    @Override
    public String importExcel(List<SysEnvironmentPoint> sysEnvironmentPoints, boolean updateSupport, String operName) {
        if (StringUtils.isNull(sysEnvironmentPoints) || sysEnvironmentPoints.size() == 0) {
            throw new ServiceException("导入用户数据不能为空！");
        }
        for (SysEnvironmentPoint sysEnvironmentPoint : sysEnvironmentPoints) {
            // 根据处置方案名称获取处置方案ID
            String planName = sysEnvironmentPoint.getPlanName();
            if (StringUtils.isNotEmpty(planName)) {
                SysPlan sysPlan = sysPlanMapper.getByName(planName);
                if (sysPlan != null) {
                    sysEnvironmentPoint.setPlanId(sysPlan.getId());
                }
            }
        }
        sysEnvironmentPointMapper.insertBatch(sysEnvironmentPoints);
        return "成功";
    }
}
